package com.hj.config.security.handler;

import com.hj.response.Resp;
import com.hj.response.RespInfo;
import com.hj.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 登录认证失败处理
 * @author hj
 */
@Component
@Slf4j
public class AuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {


    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) {
        if (exception instanceof UsernameNotFoundException) {
            ResponseUtil.responseJson(response, new Resp<>(RespInfo.NO_USERNAME));
        } else {
            ResponseUtil.responseJson(response, new Resp<>(RespInfo.UNAUTHENTICATED));
        }
    }


}
